package com.chtw.vuedemo.controller;

import com.chtw.vuedemo.bean.Comments;
import com.chtw.vuedemo.response.RespUtil;
import com.chtw.vuedemo.response.enumutil.CodeAndMsg;
import com.chtw.vuedemo.service.impl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author chtw
 * @date 2020-10-04 17:09
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentServiceImpl commentService;

    @GetMapping(value = "/list")
    public RespUtil<List<Comments>> list(int page, int p_id) {
        try{
            return new RespUtil<>(CodeAndMsg.SUCCESS, commentService.list(page, p_id));
        }catch (Exception e){
            return new RespUtil<>(CodeAndMsg.FAIL, null);
        }
    }

    @PostMapping(value = "create")
    public RespUtil<Boolean> create(@RequestBody Comments comments, HttpSession session){
        try{
            comments.setUserName((String) session.getAttribute("user_name"));
            return new RespUtil<Boolean>(CodeAndMsg.SUCCESS, commentService.create(comments));
        }catch (Exception e){
            return new RespUtil<>(CodeAndMsg.FAIL, null);
        }
    }
}
